<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="800px">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="120px"
    >
      <el-form-item label="房屋地址">
        {{ houseData.name }}
      </el-form-item>
      <el-form-item label="电费单价" prop="price" v-if="factoryCode == '0'">
        <el-input v-model.number="formData.price" placeholder="请输入电费单价">
          <template #append>元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="电价方案" prop="hxChargeTplId" v-if="factoryCode == '1'">
        <el-input v-model="formData.hxChargeTplName" :readonly="true">
          <template #append>
            <el-button @click="openHxnyPlmChargeTpl" v-if="formData.hxChargeTplId == null || formData.hxChargeTplId == undefined">选择</el-button>
            <el-button @click="clearHxnyPlmChargeTpl">清空</el-button>
          </template>
        </el-input>
        <!-- <el-input v-model="formData.hxChargeTplId" placeholder="请输入电价方案">
          <template #append>元</template>
        </el-input> -->
      </el-form-item>
      <el-form-item label="透支金额" prop="overDraft">
        <el-input v-model.number="formData.overDraft" placeholder="请输入透支金额">
          <template #append>元</template>
        </el-input>
      </el-form-item>
      <el-alert
        show-icon
        title="注意因电表集中器性能问题，在数据提交后可能会长时间处于提交状态，请关闭本页面，10分钟后在房间列表中查看电费单价和允许透支金额是否有发生变化"
        type="error"
        effect="dark"
        :closable="false"
      />
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="factoryCode == ''" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
  <HxnyPlmChargeTpl ref="hxnyPlmChargeTplRef"  @confirm-hxny-plm-charge-tpl="confirmHxnyPlmChargeTpl" />
</template>
<script lang="ts" setup>
import * as HouseRoomApi from '@/api/zwsj/houseRoom'
import * as HouseElecConfigApi from '@/api/zwsj/houseElecConfig'
import { divide } from '@/utils/tools'
import HxnyPlmChargeTpl from '@/views/zwsj/common/HxnyPlmChargeTpl.vue'

defineOptions({ name: 'HouseElecConfigSett' })

const message = useMessage() // 消息弹窗

const houseData = ref({
  id: undefined,
  name: undefined,
  commId: undefined
})
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: undefined,
  houseId: undefined,
  hxChargeTplId: undefined,
  hxChargeTplName: undefined,
  price: undefined,
  overDraft: undefined
})
const formRules = reactive({
  price: [
    {
      required: true,
      message: '电费单价不能为空',
      trigger: 'blur'
    }
  ],
  overDraft: [
    {
      required: true,
      message: '透支金额不能为空',
      trigger: 'blur'
    }
  ],
  houseId: [
    {
      required: true,
      message: '楼栋不能为空',
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref
const factoryCode = ref('')
/** 打开弹窗 */
const open = async (parentId?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '设置电表参数'
  resetForm()
  formData.value.parentId = parentId

  //判断楼栋安装电表的厂家
  factoryCode.value = await HouseElecConfigApi.getHouseElecFactoryCode(parentId)


  // 修改时，设置数据

  houseData.value = await HouseRoomApi.getHouseRoom(parentId)
  let data = await HouseElecConfigApi.getHouseElecConfig(parentId)
  if (null != data) {
    formData.value = data
    formData.value.overDraft = divide(formData.value.overDraft, 100)
    formData.value.price = divide(formData.value.price, 100)
  }
  formData.value.houseId = parentId
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as HouseElecConfigApi.HouseElecConfigVo
    await HouseElecConfigApi.saveHouseElecConfig(data)
    message.success('设置成功')
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    hxChargeTplId: undefined,
    hxChargeTplName: undefined,
    houseId: undefined,
    price: undefined,
    overDraft: undefined
  }
  formRef.value?.resetFields()
}

 
// 选择电价方案
const hxnyPlmChargeTplRef = ref()
const openHxnyPlmChargeTpl = async () => {
  hxnyPlmChargeTplRef.value.open()
}

const confirmHxnyPlmChargeTpl = async (tpl: any) => {
  formData.value.hxChargeTplId = tpl.id
  formData.value.hxChargeTplName = tpl.name
}

const clearHxnyPlmChargeTpl = async () => {
  formData.value.hxChargeTplId = undefined
  formData.value.hxChargeTplName = undefined
}
</script>
